package com.mrs.common.config;

import com.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * description: 配置跨域
 * date: 2022/7/14 13:11
 * author: MR.孙
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer{
    @Bean
    JwtInterceptor jwtInterceptor(){
        return new JwtInterceptor();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //设置放行哪些原始域
                .allowedOriginPatterns("*")
                //放行哪些原始请求头部信息
                .allowedHeaders("*")
                //是否发送Cookie
                .allowCredentials(true)
                //放行哪些请求方式
                .allowedMethods("PUT","GET","DELETE","POST")
                .maxAge(3600);

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor()).addPathPatterns("/**")  // 拦截所有请求，通过判断token是否合法来决定是否需要登录
                .excludePathPatterns("/user/login", "/user/register", "/**/export",
                        "/**/import","/**/upload","/img/**","/file/**","/**/echarts");
    }


}
